package com.cyandpos.pad.pickhangup;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.cyandpos.pad.R;
import com.cyandpos.pad.common.commondata.ErpCommonData;
import com.cyandpos.pad.posdata.TicketInfo;
import com.cyandpos.pad.utils.MoneyUtils;
import com.cyandpos.pad.utils.ToastUtils;
import com.cyandpos.pad.utils.log.SDTools;

import java.util.List;


public class PickHangUpTrade extends BaseDialog implements View.OnClickListener {

    private ListView lv_ticket, lv_goodsList;
    private HangUpListAdapter ticketListAdapter;
    private Context context;
    private TextView tv_total_money, tv_total_count;
    private IOnPickListener listener;
    private int selectPosition = -1;

    public PickHangUpTrade(Context context, IOnPickListener listener) {
        super(context, R.style.Dialog);
        this.context = context;
        this.listener = listener;
    }

    public interface IOnPickListener {
        void onPickClick();
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pick_hangup);

        initView();

    }


    private void initData() {
        List<String> stringList = SDTools.traverseFolderPick();

        for (int i=0;i<stringList.size();i++){
            Log.i("TAG----挂单信息",stringList.get(i).toString());
        }

        if (stringList.size() <= 0) {
            this.dismiss();
            ToastUtils.showMsg("无挂单信息");
        }

        ticketListAdapter = new HangUpListAdapter(getContext(), stringList);
        lv_ticket.setAdapter(ticketListAdapter);

//        for (String ss : stringList) {
//            String[] split = ss.split("_");
//            L.i(split[0] + "   " + split[1]);
//        }
    }

    private void initView() {
        findViewById(R.id.btn_pick_hangup_back).setOnClickListener(this);
        findViewById(R.id.btn_pick_hangup_pick).setOnClickListener(this);
        findViewById(R.id.btn_pick_hangup_delete).setOnClickListener(this);

        lv_ticket = (ListView) findViewById(R.id.lv_hangup_info);
        lv_goodsList = (ListView) findViewById(R.id.lv_goods_info);

        lv_ticket.setOnItemClickListener(onItemClickListener);


        tv_total_count = (TextView) findViewById(R.id.tv_total_count);
        tv_total_money = (TextView) findViewById(R.id.tv_total_money_in_ticket);

    }

    @Override
    public void show() {
        super.show();
        initData();
    }

    AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectPosition = position;
            ticketListAdapter.setSelectPosition(position);
            ticketListAdapter.notifyDataSetChanged();
            TicketInfo ticketInfo = SDTools.readTicketPick((String) ticketListAdapter.getItem(position));

            GoodsListAdapter goodsListAdapter = new GoodsListAdapter(getContext(), ticketInfo);
            lv_goodsList.setAdapter(goodsListAdapter);

            refresh(ticketInfo);
        }
    };

    private void refresh(TicketInfo ticket) {
         int TotalCount=0;
         int TotalMoney=0;
        for (int  i=0; i<ticket.getSubTickets().size();i++){
            TotalCount+=ticket.getSubTickets().get(i).getTotalCount();
            TotalMoney+=ticket.getSubTickets().get(i).getTotalMoney();
        }

        tv_total_count.setText("共 "+TotalCount+" 件");
       // tv_total_money.setText(""+TotalMoney/100);
        tv_total_money.setText(MoneyUtils.getMoneyStr(TotalMoney));


    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_pick_hangup_back: {
                this.dismiss();
            }
            break;

            case R.id.btn_pick_hangup_pick: {
                if(selectPosition < 0){
                    ToastUtils.showMsg("请先选择挂单信息");
                    return;
                }
                if (ErpCommonData.ticketInfo.getTicketGoodCount()>0){
                    ToastUtils.showMsg("商品列表有商品 ，不可取单");
                    return;
                }
                TicketInfo ticketInfo = SDTools.readTicketPick((String)ticketListAdapter.getItem(selectPosition));
                ErpCommonData.ticketInfo =null;
                ErpCommonData.ticketInfo = ticketInfo;
                listener.onPickClick();
                deleteTicket();
                this.dismiss();
            }
            break;

            case R.id.btn_pick_hangup_delete: {
                deleteTicket();
                this.dismiss();
            }
            break;
        }
    }

    private void deleteTicket() {
        if (selectPosition < 0) {
            return;
        }
        SDTools.deleteTicketPick((String) ticketListAdapter.getItem(selectPosition));
        listener.onPickClick();
//        List<String> stringList = SDTools.traverseFolderPick();
//        SDTools.deleteAllTicketPick(stringList);
    }
}
